package com.mengtian_tech.tju.dataquery.api.config;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;

@Configuration
@EnableJpaRepositories(basePackages = {
		"com.mengtian_tech.tju.dataquery.api.repo.dataAI" }, 
		transactionManagerRef = "dataAIJPATxManager", 
		entityManagerFactoryRef = "entityManagerFactoryDataAI"

)
@EnableTransactionManagement
public class JPADataAIConfig {

	@Autowired
	@Qualifier("dataSourceDataAi")
	DataSource dataSource;

	@Bean(name = "entityManagerDataAI")
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
		return primaryEntityManagerFactory(builder).getObject().createEntityManager();
	}

	// 配置jpa
	@Primary
	@Bean(name = "entityManagerFactoryDataAI") // 指定Bean的名称。
	public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder) { // 使用@Qualifier指定数据源。

		HibernateProperties hibernateProperties = new HibernateProperties();
		hibernateProperties.setDdlAuto("update");

		JpaProperties jpaProperties = new JpaProperties();
		jpaProperties.setShowSql(true);
		jpaProperties.setDatabase(Database.MYSQL);
		jpaProperties.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
		jpaProperties.setGenerateDdl(true);
		jpaProperties.setOpenInView(true);

		Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),
				new HibernateSettings());

		
		properties.put("spring.jpa.properties.hibernate.hbm2ddl.aut", "update");

		LocalContainerEntityManagerFactoryBean bean = builder.dataSource(dataSource)
				.packages("com.mengtian_tech.tju.dataquery.api.model.dataAIMysql")
				.persistenceUnit("primary")
				.properties(properties)
				.build(); // 指定实体类包和持久化单元名称。

		return bean;
	} 

	@Bean(name = "dataAIJPATxManager")
	public PlatformTransactionManager dataAIJPATxManager(
			@Qualifier("entityManagerFactoryDataAI") EntityManagerFactory fac) {
		return new JpaTransactionManager(fac);
	}

}
